package com.demo.chapter6collect;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;

import com.demo.domain.Dish;

import static java.util.stream.Collectors.*;

public class CollectorsDemo {

	public static void demo(){
		//获取菜品总数
		Dish.menu.stream().collect(counting());
		Dish.menu.stream().count();

		//获取热量最大的菜
		//Comparator<Dish> com = (a, b) -> a.getCalories() > b.getCalories() ? 1 : 0;
		Comparator<Dish> com = Comparator.comparingInt(Dish::getCalories);
		Optional<Dish> op = Dish.menu.stream().collect(maxBy(com));
		System.out.println(op.get());
	}

	public static void demo2(){
		//返回热量总和
		int total = Dish.menu.stream().collect(summingInt(Dish::getCalories));
		//返回热量平均值
		double avg = Dish.menu.stream().collect(averagingInt(Dish::getCalories));
		//返回热量总和、平均值、最大、最小值
		IntSummaryStatistics stat = Dish.menu.stream().collect(summarizingInt(Dish::getCalories));
		//用joining连接字符
		String str = Dish.menu.stream().map(Dish::getName).collect(joining(","));
	}

	public static void main(String[] args) {
		demo();
	}

}
